package cn.chencq.dataStructure.stack;

public class MyStack {

	private int maxSize;// max size

	private Object[] arr;

	private int top;// top index

	public MyStack() {
		this.top = -1;
		this.maxSize = 10;
		this.arr = new Object[maxSize];
	}

	public int size() {
		return this.top;
	}

	public MyStack(int maxSize) {
		this.top = -1;
		this.maxSize = maxSize;
		this.arr = new Object[maxSize];
	}

	public Object pop() {
		if (isEmpty()) {
			System.err.println("stack is empty.");
			return null;
		} else {
			Object o = arr[top];
			arr[top] = null;
			top--;
			return o;
		}
	}

	public void push(Object obj) {
		top++;
		if (top < maxSize) {
			arr[top] = obj;
		} else {
			System.err.println("stack full " + top);
		}
	}

	public Object peek() {
		return arr[top];
	}

	public boolean isFull() {
		return top == maxSize - 1;
	}

	public boolean isEmpty() {
		return top == -1;
	}

	public static void main(String[] args) {
		MyStack stack = new MyStack();
		stack.push("sss");
		stack.push("bbb");
		stack.push("ccc");
		stack.push("ddd");
		stack.push("eee");
		stack.push("ggg");

		while (!stack.isEmpty()) {
			System.out.println(stack.pop());
		}
	}
}
